package com.jshcbd.modules.asset.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jshcbd.framework.vo.Result;
import com.jshcbd.framework.query.QueryGenerator;
import com.jshcbd.modules.asset.dto.ReceiveDto;
import com.jshcbd.modules.asset.entity.ReceiveItemEntity;
import com.jshcbd.modules.asset.service.IAssetService;
import com.jshcbd.modules.asset.service.IAssetUseService;
import com.jshcbd.modules.asset.vo.AssetUseVo;
import com.jshcbd.modules.asset.vo.AssetVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zhoucd
 * @date
 * @description
 */
@Slf4j
@Api(tags="资产领用")
@RestController
@RequestMapping("/assetuse")
public class AssetUseController {

    @Autowired
    private IAssetUseService assetUseService;

    @Autowired
    private IAssetService assetService;


    @ApiOperation("资产领用列表")
    @GetMapping("/recevieList")
    public Result<IPage<AssetVo>> queryPageRecevieList(AssetVo assetEntity, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                       @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req) {
        QueryWrapper<AssetVo> queryWrapper = QueryGenerator.initQueryWrapper(assetEntity, req.getParameterMap());
        queryWrapper.eq("state",1);
        queryWrapper.eq("use_state",1);
        Page<AssetVo> page = new Page(pageNo, pageSize);
        Result<IPage<AssetVo>> result = new Result<>();
        IPage<AssetVo> pageList= assetService.pageView(page, queryWrapper);
        result.setData(pageList);
        log.info(pageList.toString());

        return result;
    }


    @ApiOperation("资产领用")
    @PostMapping("/recevie")
    public Result recevie(@RequestBody ReceiveDto recevieDto) {
        try {
            assetService.doReceive(recevieDto);
        } catch (Exception e) {
           return Result.error(e.getMessage());
        }
        return Result.ok("领用成功！");
    }



    @ApiOperation("获取资产领取记录")
    @GetMapping(value = {"/listByMainId/{assetId}","listByMainId"})
    public Result<IPage<AssetUseVo>> listByMainId(@PathVariable(required = false)  String assetId, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                     @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req) {
        Result<IPage<AssetUseVo>> result = new Result();
        Page<AssetUseVo> page = new Page(pageNo, pageSize);

        IPage<AssetUseVo> pageList = assetUseService.pageViewByAssetId(page,assetId);
        result.setData(pageList);
        return result;
    }

    @ApiOperation("资产领取记录")
    @GetMapping("/list")
    public Result<IPage<AssetUseVo>> list(AssetUseVo assetUseVo, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req) {
        Result<IPage<AssetUseVo>> result = new Result();
        QueryWrapper<AssetUseVo> queryWrapper = QueryGenerator.initQueryWrapper(assetUseVo, req.getParameterMap());
        Page<AssetUseVo> page = new Page(pageNo, pageSize);
        IPage<AssetUseVo> pageList = assetUseService.pageView(page,queryWrapper);
        result.setData(pageList);
        return result;
    }

    @ApiOperation("资产领取记录明细")
    @GetMapping("/listItemByMainId")
    public Result<IPage<AssetVo>> listItemByMainId(ReceiveItemEntity itemEntity, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                          @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req) {
        Result<IPage<AssetVo>> result = new Result();
        IPage<AssetVo> assetVoPage = new Page(pageNo, pageSize);
        Page page = new Page(pageNo, pageSize);
        if(itemEntity.getMainId() !=null){
            IPage<ReceiveItemEntity> pageList = assetUseService.pageUseItemsByMainId(page,itemEntity.getMainId());
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.in("id",pageList.getRecords().stream().map(rec -> rec.getAssetId()).toArray());
            assetVoPage.setTotal(pageList.getTotal());
            assetVoPage.setPages(pageList.getPages());
            assetVoPage.setRecords(assetService.listView(queryWrapper));
            result.setData(assetVoPage);
        }else{
            result.setData(assetVoPage);
        }
        return result;
    }


}
